<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <p>时间过滤器</p>
    <p>年月日：{{nowTime | timeFormat('yyyy-mm-dd')}}</p>
    <p>时分秒：{{nowTime | timeFormat('hh-mm-ss')}}</p>
    <p>周几：{{nowTime | timeFormat('w')}}</p>
  </div>
  <script src="../vue.js"></script>
  <script>
    let vm = new Vue({
      el: "#app",
      data: {
        nowTime: new Date()
      },
      // 局部注册过滤器
      filters: {
        timeFormat(val, type) {
          // type表示格式的类型
          if(type === 'yyyy-mm-dd') { // 年月日
            return `${val.getFullYear()}-${val.getMonth()+1}-${val.getDate()}`
          }else if(type === 'hh-mm-ss') { // 时分秒
            return `${val.getHours()}:${val.getMinutes()}:${val.getSeconds()}`
          }else if(type === 'w') { // 周几
            return `星期${val.getDay()}`
          }else {
            return val
          }
        }
      }
    })
  </script>
</body>
</html>